package com.ryskoo.controller.order;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ryskoo.catering.orderappraise.domain.OrderAppraisePO;
import com.ryskoo.catering.orderappraise.service.IOrderAppraiseService;
import com.ryskoo.catering.productcate.domain.ProductCategoryPO;
import com.ryskoo.customer.vendor.service.IVendorService;
import com.ryskoo.framework.core.bean.Pager;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.bean.ReturnCodeEnum;
import com.ryskoo.framework.core.bean.ReturnMessageDTO;
import com.ryskoo.framework.core.controller.BasicHandler;
import com.ryskoo.framework.core.utils.LoginUtil;

@Controller
@RequestMapping("/vendor/cater/orderappraise")
public class OrderAppraiseController extends BasicHandler<OrderAppraisePO> {
    private static final String TEMPLATE_PATH = "system/orderappraise";

    private static final String MODULE_NAME = "OrderAppraise";
    /**
     * 基类构造方法.
     */
    public OrderAppraiseController() {
        super(TEMPLATE_PATH, MODULE_NAME);
    }

    private IOrderAppraiseService orderAppraiseService;

    @Autowired
    private IVendorService vendorService;

    /**
     * 消费者注入
     *
     * @param productCateConsumer 消费者基类
     */
    @Autowired
    protected void setService(IOrderAppraiseService orderAppraiseService) {
        this.orderAppraiseService = orderAppraiseService;
        super.setService(orderAppraiseService);
    }
    
    
    @Override
	protected void buildListPageData(Model model, HttpServletRequest request) {
    	String vendorId = LoginUtil.getLoginVendorUuid();
		model.addAttribute("vendorId", vendorId);
	}


	@Override
	protected void buildAddPageData(Model model, HttpServletRequest request) {
		String vendorId = LoginUtil.getLoginVendorUuid();
		model.addAttribute("vendorId", vendorId);
	}




	@Override
	protected void buildEditPageData(Model model, OrderAppraisePO po, HttpServletRequest request) {
		String vendorId = LoginUtil.getLoginVendorUuid();
		model.addAttribute("vendorId", vendorId);
	}

	
    /**
     * 订单评论审核
     * @param id
     * @return
     */
    @PostMapping("/audit")
    @ResponseBody
    public ReturnMessageDTO<OrderAppraisePO> audit(@RequestParam String state,@RequestParam String uuid, HttpServletRequest request, HttpServletResponse response) {
    	ReturnMessageDTO<OrderAppraisePO> returnMessage = new ReturnMessageDTO<>();
        try {
            OrderAppraisePO m = orderAppraiseService.audit(state, uuid);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("审核成功");
            returnMessage.setReturnMessage(m);
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("审核失败");
        }
        return returnMessage;
    }
    
    
    /**
     * 订单评论的商家回复
     * @param id
     * @return
     */
    @PostMapping("/reply")
    @ResponseBody
    public ReturnMessageDTO<OrderAppraisePO> reply(@RequestParam String replyContent,@RequestParam String uuid) {
    	ReturnMessageDTO<OrderAppraisePO> returnMessage = new ReturnMessageDTO<>();
        try {
            OrderAppraisePO m = orderAppraiseService.getByUuid(uuid);
            if(m != null){
            	m.setReplyContent(replyContent);
            	orderAppraiseService.update(m);
            }
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnMessage(m);
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
        }
        return returnMessage;
    }

}
